package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"vehicle-mgr/handles"
)

func Init() *gin.Engine {
	// 1.创建路由
	r := gin.Default()
	r.POST("/login", handles.Login)
	r.POST("/userRole", handles.UserRole)
	r.POST("/roleNode", handles.RoleNode)
	r.GET("/userRoleList", handles.UserRoleList)
	r.GET("/roleNodeList", handles.RoleNodeList)
	r.GET("/list", handles.List)
	r.GET("/detail", handles.Detail)

	r.GET("/carList", handles.CarList)
	r.GET("/carDetail", handles.CarDetail)

	r.POST("/carAdd", handles.CarAdd)
	r.POST("/conAdd", handles.ConAdd)
	r.GET("/carConList", handles.CarConList)
	r.GET("/carConDetail", handles.CarConDetail)

	//////////物流配送//////////////
	r.POST("/perAdd", handles.PerAdd)
	r.POST("/perOrder", handles.PerOrder)
	r.GET("/perOrderList", handles.PerOrderList)
	r.GET("/perOrderDetail", handles.PerOrderDetail)

	//////////商品订单业务///////////////////
	r.POST("/goodAdd", handles.GoodAdd)
	r.POST("/goodOrderAdd", handles.GoodOrderAdd)
	r.GET("/goodOrderList", handles.GoodOrderList)
	r.GET("/goodOrderDetail", handles.GoodOrderDetail)

	///////仓库订单///////////////////
	r.POST("/houseAdd", handles.HouseAdd)
	r.POST("/houseOrderAdd", handles.HouseOrderAdd)
	r.GET("/houseOrderList", handles.HouseOrderList)
	r.GET("/houseOrderDetail", handles.HouseOrderDetail)
	r.Use(Cors()) //默认跨域

	return r
}

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
